#pragma once

#include <string>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

class Util
{
public:
    static bool ReadFile(const std::string &file_path, std::string &file_message)
    {
        // 获取文件本身的大小 stat
        struct stat buffer;
        int n = stat(file_path.c_str(), &buffer);
        if (n < 0)
            return false;
        // 调整返回值的大小
        size_t size = buffer.st_size;
        file_message.resize(size);
        // 读取
        int fd = open(file_path.c_str(), O_RDONLY);
        if (fd < 0)
            return false;
        read(fd, (char *)file_message.c_str(), file_message.size());
        close(fd);

        return true;
    }

    static std::string ReadHead(std::string &request, const std::string &SEP)
    {
        auto pos = request.find(SEP);

        std::string ret = request.substr(0, pos);
        request.erase(0, pos + SEP.size()); // 取完后就删除

        return ret;
    }

    static void PaueseLine(const std::string &line, std::string *method, std::string *url, std::string *version)
    {
        std::stringstream ss(line);
        ss >> *method >> *url >> *version;
    }
};